import http from "@/http";
import { UserInfo } from "@/store";

/**
 * @description: 用户登录
 */
export const login = (data: {
  password?: string;
  username?: string;
}): Promise<UserInfo> => {
  return http.postReq({ url: "/api/user/login", data }).then((res) => res.body);
};
/**
 * @description: 用户注册
 */
export const register = (data: {
  applyPurpose?: string[];
  companyAddress?: string;
  companyName?: string;
  companyType?: string;
  credentials?: string[];
  email?: string;
  name?: string;
  password?: string;
  phone?: string;
  professionalField?: string;
}) => {
  return http.postReq({ url: "/api/user/register", data });
};
/**
 * @description: 登录注销
 */
export const logout = () => {
  return http.getReq({ url: "/api/user/logout" });
};
/**
 * @description: 根据查询条件返回用户列表
 */
export const getUserList = (data: {
  current: number;
  pageSize: number;
  keyword?: string;
  role?: string;
  status?: string;
}) => {
  const { current, pageSize, ...rest } = data;
  return http
    .postReq({
      url: "/api/user/list",
      params: { page: current, size: pageSize },
      data: rest,
    })
    .then((res) => {
      const { body, total } = res;
      return {
        list: body,
        total,
      };
    });
};
/**
 * @description: 用户审核通过
 */
export const approveUser = (userid: string) => {
  return http.getReq({ url: "/api/user/approve/" + userid });
};
/**
 * @description: 用户审核拒绝
 */
export const rejectUser = (userid: string) => {
  return http.getReq({ url: "/api/user/reject/" + userid });
};
/**
 * @description: 获取当前用户信息
 */
export const getUserInfo = (): Promise<{
  nickname: string;
  roles: ("PROJECT" | "AUDIT" | "INDICATOR" | "ADMIN")[];
  token: string;
  userId: number;
  username: string;
}> => {
  return http.getReq({ url: "/api/user/me" }).then((res) => res.body);
};
/**
 * @description: 给项目分配审核专家对话框用
 */
export const getReviewer = (data: {
  current: number;
  pageSize: number;
  keyword?: string;
}) => {
  const { current, pageSize, ...rest } = data;
  return http
    .postReq({
      url: "/api/user/search",
      params: { page: current, size: pageSize },
      data: rest,
    })
    .then((res) => {
      const { body, total } = res;
      return {
        list: body,
        total,
      };
    });
};
